package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    @RequiresPermissions(value={"字典列表","systemDictionary:list"},logical= Logical.OR)
    public String list(QueryObject queryObject, Model model){
        PageInfo<SystemDictionary> pageResult = systemDictionaryService.selectByQuery(queryObject);
        //List<SystemDictionary> list = systemDictionaryService.getAll();
        model.addAttribute("result",pageResult);
        return "systemDictionary/list";
    }



//    @RequestMapping("/input")
//    @RequiredPermission({"字典添加","systemDictionary:input"})
//    public String input(Long id, Model model){
//        if(id != null){
//            SystemDictionary systemDictionary = systemDictionaryService.getSystemDictionaryById(id);
//            model.addAttribute("entity",systemDictionary);
//        }
//        return "systemDictionary/input";
//    }
    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value={"字典保存或更新","systemDictionary:saveOrUpdate"},logical= Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(SystemDictionary systemDictionary){
        try {
            systemDictionaryService.saveOrUpdate(systemDictionary);
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value={"字典删除","systemDictionary:delete"},logical= Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            systemDictionaryService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }
}
